package cn.mogugo.joke.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.mogugo.comm.BaseAction;
import cn.mogugo.comm.CC;
import cn.mogugo.comm.PageSupport;
import cn.mogugo.joke.action.form.JokeObjForm;
import cn.mogugo.joke.model.JokeCategory;
import cn.mogugo.joke.model.JokeObj;
import cn.mogugo.joke.model.JokeWeb;
import cn.mogugo.util.tools.NumberTools;
import cn.mogugo.util.tools.Page;

public class JokeObjAction  extends BaseAction{
	/**
	 * 默认方法
	 */
	public ActionForward unspecified(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		return list0(mapping,form,request,response);
	}
	
	
	public ActionForward list0(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		
		String strPageNo = request.getParameter("pageNo");
		Integer pageNo = NumberTools.formatObject2IntDefaultParamNoExp(strPageNo, 1);
		
		Map map = new HashMap();
		String strCategoryId = request.getParameter("categoryId");
		if(StringUtils.isNotBlank(strCategoryId)){
			map.put("categoryId", Integer.valueOf(strCategoryId));
		}
		
		JokeObjForm jokeObjForm = (JokeObjForm)form;
		String id = jokeObjForm.getId();
		if(StringUtils.isNotBlank(id)){
			map.put("id", Integer.valueOf(id));
		}
		
		
		String jokeObjTitle = jokeObjForm.getJoke_title();
		if(StringUtils.isNotBlank(jokeObjTitle)){
			map.put("joke_title", jokeObjTitle);
		}
		map.put("auditing", 0);
		
		PageSupport apage = this.jokeObjManager.getPage(map, pageNo, CC.PAGE_SIZE , "new");
		request.setAttribute("page", apage.getItems());
    	request.setAttribute("pageStr", Page.getInstance().createPageStr(request, apage));
    	
    	
		return mapping.findForward("list0");
	}
	
	
	
	public ActionForward create0(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		
		List listJc = this.jokeCategoryManager.getAll();
		request.setAttribute("listJc", listJc);
		return mapping.findForward("edit0");
	}
	
	

	
	
	public ActionForward edit0(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		JokeObj jo = (JokeObj)this.baseDao.get(JokeObj.class, Integer.valueOf(id));
		JokeObjForm jokeObjForm = (JokeObjForm)form;
		BeanUtils.copyProperties(jokeObjForm ,jo);
		
		if(jo.getJokeCategory() != null){
			jokeObjForm.setCategory_id(jo.getJokeCategory().getId());	
		}
		if(jo.getJokeWeb() != null){
			jokeObjForm.setWeb_id(jo.getJokeWeb().getId());
		}
		jokeObjForm.setJoke_content(jo.getJoke_content().replaceAll("<br>", "\n"));
		request.setAttribute("jo", jo);
		
		List listJc = this.jokeCategoryManager.getAll();
		request.setAttribute("listJc", listJc);
		
		return mapping.findForward("edit0");
	}
	
	
	public ActionForward saveOrUpdate0(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		
		String type = request.getParameter("type");
		
		
		JokeObjForm jokeObjForm = (JokeObjForm)form;
		JokeObj jo;
		if(StringUtils.isNotBlank(jokeObjForm.getId())){
			jo = (JokeObj)this.baseDao.get(JokeObj.class, Integer.valueOf(jokeObjForm.getId()));
		}else{
			jo = new JokeObj();
		}
		BeanUtils.copyProperties(jo , jokeObjForm);
		jo.setJoke_content(jokeObjForm.getJoke_content().replaceAll("\\s+", "<br>"));
		if(jo.getId() == 0){
			jo.setId(null);
			jo.setAuditing(99);
			JokeWeb jw = (JokeWeb)this.baseDao.get(JokeWeb.class, 99);
			jo.setJokeWeb(jw);
			jo.setJokeCategory((JokeCategory)this.baseDao.get(JokeCategory.class, jokeObjForm.getCategory_id()));
			
		}else{
			if(jokeObjForm.getAuditing() != null){
				jo.setAuditing(jokeObjForm.getAuditing());
			}else{
				jo.setAuditing(Integer.valueOf(type));
			}
			if(jokeObjForm.getCategory_id() != null){
				jo.setJokeCategory((JokeCategory)this.baseDao.get(JokeCategory.class, jokeObjForm.getCategory_id()));
			}
		}
		this.baseDao.saveOrUpdateLog(jo);
		
		return mapping.findForward("tolist0");
	}
	
	
	
	public ActionForward delete0(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		this.baseDao.deleteObjectByIdLog(JokeObj.class, Integer.valueOf(id));
		return mapping.findForward("tolist0");
	}
	
	
	public ActionForward sameList(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String strPageNo = request.getParameter("pageNo");
		Integer pageNo = NumberTools.formatObject2IntDefaultParamNoExp(strPageNo, 1);
		
		PageSupport apage = this.jokeObjManager.getSameTitleJo(pageNo , CC.PAGE_SIZE);
		request.setAttribute("page", apage.getItems());
    	request.setAttribute("pageStr", Page.getInstance().createPageStr(request, apage));
    	
    	
		return mapping.findForward("sameList");
	}
	
	public ActionForward deleteSame(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String[] arrayStrId = request.getParameterValues("chbItemlist");
		
		this.jokeObjManager.deleteSame(arrayStrId);
		
		return mapping.findForward("toSameList");
	}
	
	public ActionForward sameTitleDiffContent(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String strPageNo = request.getParameter("pageNo");
		Integer pageNo = NumberTools.formatObject2IntDefaultParamNoExp(strPageNo, 1);
		
		PageSupport apage = this.jokeObjManager.getSameTitleJo(pageNo , 4000);
		List list = apage.getItems();
		List sameList = new ArrayList();
		
		for(int i = 0 ; i < list.size() ; i ++ ){
			Map mapOfCol = (Map)list.get(i);
			String joke_title = (String)mapOfCol.get("JOKE_TITLE");
			List listContent = jokeObjManager.getByJokeTitle(joke_title);
			if(listContent.size() == 2){
				int a = 0 ;
				int b = 0 ;
				int aa;
				int bb;
				JokeObj joA = (JokeObj)listContent.get(0);
				JokeObj joB = (JokeObj)listContent.get(1);
				String strA = joA.getJoke_content().replaceAll("\\s+", "").replaceAll("<br>", "").replaceAll("&[a-z]+;", "").replaceAll("“", "").replaceAll("　", "");
				String strB = joB.getJoke_content().replaceAll("\\s+", "").replaceAll("<br>", "").replaceAll("&[a-z]+;", "").replaceAll("“", "").replaceAll("　", "");
				
				a = strA.length();
				b = strB.length();
				if(a > b){
					aa = b;
					bb = a;
				}else{
					aa = a;
					bb = b;
				}
				
				double diffLevel = new Double(aa)/new Double(bb);
				if(diffLevel > 0.70d){
					if((strA.charAt(0)+"").equals(strB.charAt(0)+"")){
						mapOfCol.put("diffLevel", diffLevel);
						sameList.add(mapOfCol);
					}
					
				}
			}
			if(sameList.size() > 200){
				break;
			}

		}
		
		//System.out.println("sameList = " + sameList.size());
		
		request.setAttribute("page", sameList);
    	
		return mapping.findForward("sameList");
	}
	
	
	
	
	public ActionForward ignore(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		String title = request.getParameter("jokeTitle");
		List listContent = jokeObjManager.getByJokeTitle(title);
		for(int i = 0 ; i < listContent.size() ; i ++ ){
			JokeObj jo = (JokeObj)listContent.get(i);
			jo.setAuditing(2);
			this.baseDao.saveOrUpdate(jo);
		}
		return mapping.findForward("toSameList");
	}
	
	
	
	
	public ActionForward list1(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		
		String strPageNo = request.getParameter("pageNo");
		Integer pageNo = NumberTools.formatObject2IntDefaultParamNoExp(strPageNo, 1);
		
		Map map = new HashMap();
		String strCategoryId = request.getParameter("categoryId");
		if(StringUtils.isNotBlank(strCategoryId)){
			map.put("categoryId", Integer.valueOf(strCategoryId));
		}
		
		JokeObjForm jokeObjForm = (JokeObjForm)form;
		String id = jokeObjForm.getId();
		if(StringUtils.isNotBlank(id)){
			map.put("id", Integer.valueOf(id));
		}
		
		
		String jokeObjTitle = jokeObjForm.getJoke_title();
		if(StringUtils.isNotBlank(jokeObjTitle)){
			map.put("joke_title", jokeObjTitle);
		}
		map.put("auditing", 1);
		
		PageSupport apage = this.jokeObjManager.getPage(map, pageNo, CC.PAGE_SIZE , "new");
		request.setAttribute("page", apage.getItems());
    	request.setAttribute("pageStr", Page.getInstance().createPageStr(request, apage));
    	
    	
		return mapping.findForward("list1");
	}
	
	
	

}
